/*
 * Copyright (C) 2024 HiHope Open Source Organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {startMiracastServiceCallback} from "./i_miracast_player_service";
import {stopMiracastServiceCallback} from "./i_miracast_player_service";
import {registerDeviceConnectListenerCallback} from "./i_miracast_player_service";
import {unRegisterDeviceConnectListenerCallback} from "./i_miracast_player_service";
import {setSurfaceCallback} from "./i_miracast_player_service";
import {playCallback} from "./i_miracast_player_service";
import {pauseCallback} from "./i_miracast_player_service";
import {stopCallback} from "./i_miracast_player_service";
import IMiracastPlayerService from "./i_miracast_player_service";
import rpc from "@ohos.rpc";
import IDeviceConnectListener from "./i_device_connect_listener";
import DeviceConnectListenerStub from "./device_connect_listener_stub";

export default class MiracastPlayerServiceProxy implements IMiracastPlayerService {
    constructor(proxy) {
        this.proxy = proxy;
    }

    startMiracastService(callback: startMiracastServiceCallback): void
    {
        let option = new rpc.MessageOption();
        let dataSequence = rpc.MessageSequence.create();
        let replySequence = rpc.MessageSequence.create();
        this.proxy.sendMessageRequest(MiracastPlayerServiceProxy.COMMAND_START_MIRACAST_SERVICE, dataSequence, replySequence, option).then((result: rpc.RequestResult) => {
            if (result.errCode === 0) {
                let errCodeVar = result.reply.readInt();
                if (errCodeVar != 0) {
                    let returnValueVar = undefined;
                    callback(errCodeVar, returnValueVar);
                    return;
                }
                let returnValueVar = result.reply.readInt();
                callback(errCodeVar, returnValueVar);
            } else {
                console.log("sendMessageRequest failed, errCode: " + result.errCode);
            }
        }).catch((e: Error) => {
            console.log('sendMessageRequest failed, message: ' + e.message);
        }).finally(() => {
            dataSequence.reclaim();
            replySequence.reclaim();
        });
    }

    stopMiracastService(callback: stopMiracastServiceCallback): void
    {
        let option = new rpc.MessageOption();
        let dataSequence = rpc.MessageSequence.create();
        let replySequence = rpc.MessageSequence.create();
        this.proxy.sendMessageRequest(MiracastPlayerServiceProxy.COMMAND_STOP_MIRACAST_SERVICE, dataSequence, replySequence, option).then((result: rpc.RequestResult) => {
            if (result.errCode === 0) {
                let errCodeVar = result.reply.readInt();
                if (errCodeVar != 0) {
                    let returnValueVar = undefined;
                    callback(errCodeVar, returnValueVar);
                    return;
                }
                let returnValueVar = result.reply.readInt();
                callback(errCodeVar, returnValueVar);
            } else {
                console.log("sendMessageRequest failed, errCode: " + result.errCode);
            }
        }).catch((e: Error) => {
            console.log('sendMessageRequest failed, message: ' + e.message);
        }).finally(() => {
            dataSequence.reclaim();
            replySequence.reclaim();
        });
    }

    registerDeviceConnectListener(listener: IDeviceConnectListener, callback: registerDeviceConnectListenerCallback): void
    {
        let option = new rpc.MessageOption();
        let dataSequence = rpc.MessageSequence.create();
        let replySequence = rpc.MessageSequence.create();
        dataSequence.writeRemoteObject(listener as DeviceConnectListenerStub);
        this.proxy.sendMessageRequest(MiracastPlayerServiceProxy.COMMAND_REGISTER_DEVICE_CONNECT_LISTENER, dataSequence, replySequence, option).then((result: rpc.RequestResult) => {
            if (result.errCode === 0) {
                let errCodeVar = result.reply.readInt();
                if (errCodeVar != 0) {
                    let returnValueVar = undefined;
                    callback(errCodeVar, returnValueVar);
                    return;
                }
                let returnValueVar = result.reply.readInt();
                callback(errCodeVar, returnValueVar);
            } else {
                console.log("sendMessageRequest failed, errCode: " + result.errCode);
            }
        }).catch((e: Error) => {
            console.log('sendMessageRequest failed, message: ' + e.message);
        }).finally(() => {
            dataSequence.reclaim();
            replySequence.reclaim();
        });
    }

    unRegisterDeviceConnectListener(listener: IDeviceConnectListener, callback: unRegisterDeviceConnectListenerCallback): void
    {
        let option = new rpc.MessageOption();
        let dataSequence = rpc.MessageSequence.create();
        let replySequence = rpc.MessageSequence.create();
        dataSequence.writeRemoteObject(listener as DeviceConnectListenerStub);
        this.proxy.sendMessageRequest(MiracastPlayerServiceProxy.COMMAND_UN_REGISTER_DEVICE_CONNECT_LISTENER, dataSequence, replySequence, option).then((result: rpc.RequestResult) => {
            if (result.errCode === 0) {
                let errCodeVar = result.reply.readInt();
                if (errCodeVar != 0) {
                    let returnValueVar = undefined;
                    callback(errCodeVar, returnValueVar);
                    return;
                }
                let returnValueVar = result.reply.readInt();
                callback(errCodeVar, returnValueVar);
            } else {
                console.log("sendMessageRequest failed, errCode: " + result.errCode);
            }
        }).catch((e: Error) => {
            console.log('sendMessageRequest failed, message: ' + e.message);
        }).finally(() => {
            dataSequence.reclaim();
            replySequence.reclaim();
        });
    }

    setSurface(surfaceId: string, callback: setSurfaceCallback): void
    {
        let option = new rpc.MessageOption();
        let dataSequence = rpc.MessageSequence.create();
        let replySequence = rpc.MessageSequence.create();
        dataSequence.writeString(surfaceId);
        this.proxy.sendMessageRequest(MiracastPlayerServiceProxy.COMMAND_SET_SURFACE, dataSequence, replySequence, option).then((result: rpc.RequestResult) => {
            if (result.errCode === 0) {
                let errCodeVar = result.reply.readInt();
                if (errCodeVar != 0) {
                    let returnValueVar = undefined;
                    callback(errCodeVar, returnValueVar);
                    return;
                }
                let returnValueVar = result.reply.readInt();
                callback(errCodeVar, returnValueVar);
            } else {
                console.log("sendMessageRequest failed, errCode: " + result.errCode);
            }
        }).catch((e: Error) => {
            console.log('sendMessageRequest failed, message: ' + e.message);
        }).finally(() => {
            dataSequence.reclaim();
            replySequence.reclaim();
        });
    }

    play(deviceId: string, callback: playCallback): void
    {
        let option = new rpc.MessageOption();
        let dataSequence = rpc.MessageSequence.create();
        let replySequence = rpc.MessageSequence.create();
        dataSequence.writeString(deviceId);
        this.proxy.sendMessageRequest(MiracastPlayerServiceProxy.COMMAND_PLAY, dataSequence, replySequence, option).then((result: rpc.RequestResult) => {
            if (result.errCode === 0) {
                let errCodeVar = result.reply.readInt();
                if (errCodeVar != 0) {
                    let returnValueVar = undefined;
                    callback(errCodeVar, returnValueVar);
                    return;
                }
                let returnValueVar = result.reply.readInt();
                callback(errCodeVar, returnValueVar);
            } else {
                console.log("sendMessageRequest failed, errCode: " + result.errCode);
            }
        }).catch((e: Error) => {
            console.log('sendMessageRequest failed, message: ' + e.message);
        }).finally(() => {
            dataSequence.reclaim();
            replySequence.reclaim();
        });
    }

    pause(deviceId: string, callback: pauseCallback): void
    {
        let option = new rpc.MessageOption();
        let dataSequence = rpc.MessageSequence.create();
        let replySequence = rpc.MessageSequence.create();
        dataSequence.writeString(deviceId);
        this.proxy.sendMessageRequest(MiracastPlayerServiceProxy.COMMAND_PAUSE, dataSequence, replySequence, option).then((result: rpc.RequestResult) => {
            if (result.errCode === 0) {
                let errCodeVar = result.reply.readInt();
                if (errCodeVar != 0) {
                    let returnValueVar = undefined;
                    callback(errCodeVar, returnValueVar);
                    return;
                }
                let returnValueVar = result.reply.readInt();
                callback(errCodeVar, returnValueVar);
            } else {
                console.log("sendMessageRequest failed, errCode: " + result.errCode);
            }
        }).catch((e: Error) => {
            console.log('sendMessageRequest failed, message: ' + e.message);
        }).finally(() => {
            dataSequence.reclaim();
            replySequence.reclaim();
        });
    }

    stop(deviceId: string, callback: stopCallback): void
    {
        let option = new rpc.MessageOption();
        let dataSequence = rpc.MessageSequence.create();
        let replySequence = rpc.MessageSequence.create();
        dataSequence.writeString(deviceId);
        this.proxy.sendMessageRequest(MiracastPlayerServiceProxy.COMMAND_STOP, dataSequence, replySequence, option).then((result: rpc.RequestResult) => {
            if (result.errCode === 0) {
                let errCodeVar = result.reply.readInt();
                if (errCodeVar != 0) {
                    let returnValueVar = undefined;
                    callback(errCodeVar, returnValueVar);
                    return;
                }
                let returnValueVar = result.reply.readInt();
                callback(errCodeVar, returnValueVar);
            } else {
                console.log("sendMessageRequest failed, errCode: " + result.errCode);
            }
        }).catch((e: Error) => {
            console.log('sendMessageRequest failed, message: ' + e.message);
        }).finally(() => {
            dataSequence.reclaim();
            replySequence.reclaim();
        });
    }

    static readonly COMMAND_START_MIRACAST_SERVICE = 1;
    static readonly COMMAND_STOP_MIRACAST_SERVICE = 2;
    static readonly COMMAND_REGISTER_DEVICE_CONNECT_LISTENER = 3;
    static readonly COMMAND_UN_REGISTER_DEVICE_CONNECT_LISTENER = 4;
    static readonly COMMAND_SET_SURFACE = 5;
    static readonly COMMAND_PLAY = 6;
    static readonly COMMAND_PAUSE = 7;
    static readonly COMMAND_STOP = 8;
    private proxy
}

